package ca.jsbr.as2js_lib.display.canvas.bitmap 
{
	import ca.jsbr.as2js_lib.display.canvas.bitmap.imageFilter.IFilter;
	import ca.jsbr.as2js_lib.display.utils.Color;
	import ca.jsbr.as2js_lib.utils.geom.Rect;
	import js.document;
	import js.element.canvas.CanvasRenderingContext2D;
	import js.element.canvas.ImageData;
	import js.element.HTMLCanvasElement;
	import js.element.HTMLImageElement;
	import js.event.Event;
	/**
	 * ...
	 * @author jsbr [me@jsbr.ca]
	 */
	public class BitmapData 
	{
		private var _ready:Boolean;
		private var _originalData:ImageData;
		private var _imageData:ImageData;
		private var _width:int;
		private var _height:int;
		private var _image:HTMLImageElement;
		private var _canvas:HTMLCanvasElement;
		private var _context:CanvasRenderingContext2D;
		private var _filters:Vector.<IFilter>;
		private var _data:Array;
		private var _modified:Boolean;
		
		public function BitmapData(src:*=null) 
		{
			_image = src;
			_filters = new Vector.<IFilter>();
			if (_image.width == 0)
				_image.addEventListener("load", onLoad);
			else
				onLoad(null);
		}
		
		protected function onLoad(e:Event):void 
		{
			_ready=true;
			_width = _image.width;
			_height = _image.height;
			createData();
			/*if(_filter)
				_imageData = _filter.render(this);*/
			
		}
				
		
		public function clear():void
		{
			_imageData = null;
			_canvas = null;
			_context = null;
		}
		
		public function get width():int 
		{
			return _width;
		}
		
		public function set width(value:int):void 
		{
			_width = value;
		}
		
		public function get height():int 
		{
			return _height;
		}
		
		public function set height(value:int):void 
		{
			_height = value;
		}
	
		
		public function get imageData():ImageData 
		{
			if (!_imageData)
				createData();
			return _imageData;
		}
		
		public function set imageData(value:ImageData):void 
		{
			_imageData = value;
			_data = _imageData.data;
		}
		
		public function isModified():Boolean 
		{
			return _modified;
		}
		
		public function get data():Array 
		{
			return _data;
		}
		
		public function set modified(value:Boolean):void 
		{
			_modified = value;
		}
		
		public function get context():CanvasRenderingContext2D 
		{
			return _context;
		}
		
		
		public function crop(rect:Rect):void
		{
			
		}
		
		private function createData():void 
		{
			if (_width == 0 || _imageData)
				return null;
			var canvas:HTMLCanvasElement = document.createElement("canvas");
			canvas.height = _height;
			canvas.width = _width;
			document.body.appendChild(canvas);
			
			_context = canvas.getContext("2d");
			_context.drawImage(_image, 0, 0, width, height);
			
			imageData = context.getImageData(0, 0, _width, _height);
			_originalData = context.getImageData(0, 0, _width, _height);
		}
		
		public function render():void
		{
			_modified = false;
			_imageData.data = _originalData.data;
			_data = _imageData.data;
			for (var i:int = 0; i < _filters.length; i++)
			{
				imageData = _filters[i].render(this);
			}
		}
		
		public function addFilter(filter:IFilter):void
		{
			_filters.push(filter);
			_modified = true;
			imageData = filter.render(this);
		}
		
		public function  removeFilter(filter:IFilter):void
		{
			var ind:int = _filters.indexOf(filter);
			if (ind > -1)
				_filters.splice(ind, 1);
			_modified = true;
		}
		
		public function getPixelColor(x,y):Color
		{
			var index:int = (x + _width * y) * 4;
			return new Color(_data[index], _data[index + 1], _data[index + 2], _data[index + 3]);
		}
		
		//getImageData
		// createImageData
		// putImageData
		
	}

}